using Admin2025.Domain.Envents;

namespace Admin2025.Domain.Entities.App;

public class AppUser : EntityBase
{
    public string Username { get; set; } = null!;
    public string Password { get; set; } = null!;
    public string Salt { get; set; } = null!;
    public string? AvatarUrl { get; set; }
    public string? Nickname { get; set; }
    public ICollection<AppRole> Roles { get; set; } = new List<AppRole>();

    //创建新用户（静态工厂方法），这是一个重要的业务事件，需要发布事件
    public static AppUser Create(string username, string password, string salt, string? nickname = null)
    {
        var user = new AppUser
        {
            Id = Guid.NewGuid(),
            Username = username,
            Password = password,
            Salt = salt,
            Nickname = nickname,
            CreatedAt = DateTime.UtcNow,
            UpdateAt = DateTime.UtcNow
        };

        //用户创建是重要的业务条件
        user.AddDomainEvent(new UserCreateEvent(user.Id, user.Username, user.Nickname));

        return user;
    }

    //分配角色--重要的业务操作，需要事件
    public void AssignRole(AppRole role)
    {
        if (!Roles.Contains(role))
        {
            Roles.Add(role);
            AddDomainEvent(new UserRoleAssignedEvent(Id, Username, role.Id, role.Name));

        }
    }

    //移除角色 
    public void RemoveRole(AppRole role)
    {
        if (Roles.Contains(role))
        {
            Roles.Remove(role);
            AddDomainEvent(new UserRoleRemovedEEvent(Id, Username, role.Id, role.Name));
        }
    }

    //修改密码--安全相关的重要业务操作
    public void ChangePassword(string oldPwdHash, string newPwdHash)
    {
        if (Password != oldPwdHash) throw new Exception("原始密码错误");
        Password = newPwdHash;
        UpdateAt = DateTime.UtcNow;

        //密码修改是重要的安全事件
        AddDomainEvent(new UserPasswordChangedEvent(Id, Username));
    }

    //更新昵称--一般的属性更新，通常不需要事件
    public void UpdateNickname(string nickname)
    {
        Nickname = nickname;
        UpdateAt = DateTime.UtcNow;
        //昵称更新通常不是重要的业务事件，不发布事件
    }
    //更新更新--一般的属性更新，通常不需要事件
    public void UpdateAvatarUrl(string avatarUrl)
    {
        AvatarUrl = avatarUrl;
        UpdateAt = DateTime.UtcNow;
        //头像更新通常不是重要的业务事件，不发布事件
    }

    //启用用户--状态变更，可能需要事件
    public void Enable()
    {
        if (!IsActived)
        {
            IsActived = true;
            UpdateAt = DateTime.UtcNow;

            //用户状态变更可能是重要的业务事件
            AddDomainEvent(new UserStatusChangeEvent(Id, Username, "Enable"));
        }
    }
    //禁用用户--状态变更，可能需要事件
    public void Disable()
    {
        if (IsActived)
        {
            IsActived = false;
            UpdateAt = DateTime.UtcNow;

            //用户状态变更可能是重要的业务事件
            AddDomainEvent(new UserStatusChangeEvent(Id, Username, "Disable"));
        }
    }

    //软删除用户 - 重要的业务操作
    public void SoftDelete(Guid deleteBy)
    {
        IsDeleted = true;
        UpdatedAt = DateTime.UtcNow;
        UpdatedBy = deleteBy;

        //用户删除是重要的业务事件
        AddDomainEvent(new UserDeletedEvent(Id, Username, deletedBy));
    }

}